# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        博智科技  
# Name:         demo1731
# Description:
# Author:       yzl
# Date:         2019-02-08
#-------------------------------------------------------------------------------

import threading
from time import sleep,ctime

# 1 定义线程函数
def func(index,sec):
    print('开始执行',index,'执行时间',ctime())
    sleep(sec)
    print('执行结束', index, '结束时间', ctime())

def main():
    # 创建一个线程对象              线程函数    线程函数的参数
    thread1 = threading.Thread(target=func,args=(10,4))
    # 判断线程是否活跃 线程准备就绪
    if thread1.is_alive():
        print('thread1 is alive')
    else:
        print('thread1 is died')

    # 启动线程
    thread1.start()

    # 线程对象2
    thread2 = threading.Thread(target=func, args=(20, 2))
    thread2.start()

    # 判断线程1是否活跃
    if thread1.is_alive():
        print('thread1 is alive')
    else:
        print('thread1 is over')

    print("main is waiting...")

    # 等待线程执行完毕 主线程在等待所有的线程执行完毕后执行后面的代码
    thread1.join()
    thread2.join()

    print('all thread is over.')

    # 判断线程1是否活跃
    if thread1.is_alive():
        print('-->thread1 is alive')
    else:
        print('-->thread1 is over')

    # 检查线程对象2的状态
    if thread2.is_alive():
        print('thread2 is alive')
    else:
        print('thread2 is over')

    print("main is over")


if __name__ == '__main__':
    main()
